import { create } from 'zustand';
import { register } from '@/services';

const store = create((set, get) => ({
  open: {},
  loading: {
    register: false
  },
  userData: {},

  register: async (params) => {

    const response = await register.register(params);
    console.log(response);

    // 更新loading状态
    set({ loading: { register: false } });

    // 判断是否有token
    if (response.status === 1 && response.token) {
      // 保存token到localStorage
      localStorage.setItem('token', response.token);

      // 保存用户数据到store
      set({
        userData: response.user
      });
      set({ loading: { register: false } });
      // 返回成功状态
      return {
        success: true,
        data: response
      };
    } else {
      // 返回失败状态
      set({ loading: { register: false } });
      return {
        success: false,
        message: response.data.error.error
      };
    }
  },



  updates: (params) => {
    set((state) => ({ ...state, ...params }));
  },

  clearLoginData: () => set({
    userData: {},
    loading: { register: false }
  }),
}));

export default store;